$(function(){
	var borderStyleDefault={'borderLeft':'3px solid transparent'};
	var borderStyleSelected={'borderLeft':'3px solid '+$('nav#nav').css('backgroundColor')};
	var color=$('nav#nav').css('backgroundColor');
	var $entries=null;
	var lastUrl=location.href;

	var _dommod__Wrapper=function(e){
		if(view()=='time'&&(action()=='day'||action()=='week')&&lastUrl!=location.href)resetEntries();
	}
	var _keydown__Document=function(e){
		var k=e.which||e.keyCode,a=document.activeElement,$a=$(a);
		var time_day=function(k,s,alt,$a){
			if($.inArray(k,[13,74,75,77,79,84,85,87])==-1)return; // [return],j,k,m,o,t,u,w
			if(lastUrl!=location.href)resetEntries();

			var $e=$('li.day-view-entry:visible'),$s=$e.filter('.selected'),idx=$s.index();
			switch(k){
				case 13: // [return]
					if($s.length==0)return false;
					var $b=$s.find('a.js-start-timer:visible,a.js-stop-timer:visible');
					if($b.length>0)$b.get(0).click();
					return false;
					break;
				case 74: // j
					if(s)$('a.js-jump-one-day-forward:visible').get(0).click();
					else{
						if($s.length==0)$e.filter(':first').css(borderStyleSelected).addClass('selected');
						else if(idx!=($e.length-1))$s.css(borderStyleDefault).removeClass('selected').next().css(borderStyleSelected).addClass('selected');
					}
					return false;
					break;
				case 75: // k
					if(s)$('a.js-jump-one-day-back:visible').get(0).click()
					else{
						if($s.length==0)$e.filter(':first').css(borderStyleSelected).addClass('selected');
						else if(idx!=0)$s.css(borderStyleDefault).removeClass('selected').prev().css(borderStyleSelected).addClass('selected');
					}
					return false;
					break;
				case 77: // m
					if(s)return false;
					$('a.js-calendar:visible').get(0).click();
					return false;
					break;
				case 79: // o
					if(s)return false;
					if($s.length==0)return false;
					var $b=$s.find('a.js-edit-entry');
					if($b.length>0)$b.get(0).click();
					return false;
					break;
				case 84: // t
					if(s)return false;
					$('a.jump-to-today:visible').get(0).click();
					return false;
					break;
				case 85: // u
					var $a=$('a.js-other-user-btn:visible'),$s=$('select.js-other-user-selector').not('sickled');
					if(s)return false;
					if($a==undefined)return false;
					$s.change(function(){return false}).addClass('sickled').keydown(_keydown__UserSelect);
					$a.get(0).click();
					return false;
					break;
				case 87: // w
					if(s)return false;
					$('a.test-nav-week:visible').get(0).click();
					return false;
					break;
			}
		}
		var time_week=function(k,s,alt,$a){
			if($.inArray(k,[51,68,74,75,77,84,85])==-1)return $a[0].tagName=='INPUT'; // 3,d,j,k,m,t,u
			if(lastUrl!=location.href)resetEntries();
			if($('a.js-cancel-delete-entry:visible').length>0)$('a.js-cancel-delete-entry:visible').get(0).click();
			if($('div.calendar:visible').length>0)$('a.js-calendar:visible').get(0).click();

			var $e=$('table.week-view-table>tbody').find('td.name:visible'),$s=$e.filter('.selected'),idx=$e.index($s),f=$(document.activeElement).hasClass('js-compound-entry');
			
			
			switch(k){
				case 51: // 3
					if(!s)return;
					var $b=$s.parents('tr:first').find('a.week-row-delete');
					$(document.activeElement).blur();
					if($b.length>0)$b.get(0).click();
					$('a.js-cancel-delete-entry:visible').focus();
					return false;
					break;
				case 68: // d
					if(alt)return true;
					if(s)return false;
					var i=$s.parents('tr:first').find('input').index(document.activeElement);
					if(i==-1)$('a.test-nav-day:visible').get(0).click();
					else $s.parents('table:first').children('thead').find('a.js-day:eq('+i+')').get(0).click();
					return false;
					break;
				case 74: // j
					if(s)$('a.js-jump-one-week-forward:visible').get(0).click();
					else{
						if($s.length==0)time_week_select($e.filter(':first').parents('tr:first'));
						else if(idx!=($e.length-1))time_week_select($s.parents('tr:first').next());
					}
					return false;
					break;
				case 75: // k
					if(s)$('a.js-jump-one-week-back:visible').get(0).click()
					else{
						var $c=$(document.activeElement);
						if($s.length==0)time_week_select($e.filter(':first'));
						else if(idx!=0)time_week_select($s.parents('tr:first').prev());
					}
					return false;
					break;
				case 77: // m
					if(s)return false;
					$('a.js-calendar:visible').get(0).click();
					return false;
					break;
				case 84: // t
					if(s)return false;
					$('a.jump-to-this-week:visible').get(0).click();
					return false;
					break;
				case 85: // u
					var $a=$('a.js-other-user-btn:visible'),$s=$('select.js-other-user-selector').not('sickled');
					if(s)return false;
					if($a==undefined)return false;
					$s.change(function(){return false}).addClass('sickled').keydown(_keydown__UserSelect);
					$a.get(0).click();
					return false;
					break;
			}

			return true;
		}

		if(e.ctrlKey)return;
		if(document.activeElement.className.indexOf('js-compound-entry')==-1&&{"INPUT":true,"SELECT":true,"TEXTAREA":true}[document.activeElement.tagName])return;
		if(e.shiftKey&&$.inArray(k,[69,73,77,82,84])>-1&&$.inArray(a,['INPUT','SELECT','TEXTAREA'])==-1){ // e,i,m,r,t
			var $el;

			switch(k){
				case 69:$el=$('ul.nav-tabs').find('a:contains(Estimate)');break;
				case 73:$el=$('ul.nav-tabs').find('a:contains(Invoices)');break;
				case 77:$el=$('ul.nav-tabs').find('a:contains(Manage)');break;
				case 82:$el=$('ul.nav-tabs').find('a:contains(Reports)');break;
				case 84:$el=$('ul.nav-tabs').find('a:contains(Timesheets)');break;
				default:$el=$();
			}
			if($el.length>0){$el.get(0).click();return false;}
		}
		else{
			switch(action()){
				case 'day':return time_day(k,e.shiftKey,e.altKey,$a);break;
				case 'week':return time_week(k,e.shiftKey,e.altKey,$a);break;
			}
		}
	}
	var _keydown__UserSelect=function(e){
		var k=e.which||e.keyCode,a=document.activeElement,$a=$(a);
		if(k==27)$('a.js-other-user-btn:visible').focus().get(0).click();
		if(k==13){
			var $t=$(this),d=((action()=='day')?$('div.day-view-wrapper:visible'):$('div.week-view-wrapper:visible')).find('h1').attr('class').split(' ')[0].split('-')[2];
			location.href='/time/'+action()+'/'+d.substring(0,4)+'/'+d.substring(4,6)+'/'+d.substring(6,8)+'/'+$t.blur().val();
		}
		return true;
	}
	var action=function(){
		if(location.href.split('/').length<5){
			switch(view()){
				case 'time':return 'day';break;
				default: return 'none';break;
			}
		}
		return location.href.split('/')[4].toLowerCase();
	}
	var init=function(){
		$('div.wrapper').bind('DOMSubtreeModified',_dommod__Wrapper);
		$(document).keydown(_keydown__Document);
		resetEntries();
	}
	var resetEntries=function(){
		$entries=(action()=='day')?$('li.day-view-entry'):$('table.week-view-table>tbody').find('td.name');
		$entries.removeClass('sickled_border').removeClass('selected').css(borderStyleDefault);
		$entries=$entries.filter(':visible');
		$entries.css(borderStyleDefault).addClass('sickled_border');
		lastUrl=location.href;
		$('input.js-compound-entry:visible').not('.sickled_click').addClass('sickled_click').click(function(){time_week_select($(this).parents('tr:first'))});
		if($(document.activeElement).length>0)document.activeElement.blur();
	}
	var time_week_select=function($e){
		var $a=$(document.activeElement);
		var $c=$a.hasClass('js-compound-entry')?$a:$e.find('input.js-compound-entry:first');
		var i=$c.parents('tr:first').find('input.js-compound-entry').index($c);

		i=(i==-1)?0:i;
		$e.parents('tbody:first').find('td.selected').removeClass('selected').css(borderStyleDefault);
		$e.find('td.name:first').css(borderStyleDefault).addClass('selected').css(borderStyleSelected);
		$e.find('input.js-compound-entry:eq('+i+')').select().focus();
	}
	var view=function(){
		return location.href.split('/')[3].toLowerCase();
	}

	init();
});